package com.colldown.iservice.dataShow.web.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.colldown.common.A_Tools;
import com.colldown.iservice.dataShow.service.IDataShowService;
import com.colldown.iservice.dataShow.web.IDataGrid;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@Controller
@RequestMapping("/dataShow")
public class DataGridImpl implements IDataGrid {
	@Autowired
	IDataShowService dataShowService;

	@Override
	@RequestMapping("/queryAddrDateCharts")
	public String queryAddrDateCharts(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//是否为ajax取数据标志
		String ajaxFlag = A_Tools.NullToStr(request.getParameter("ajaxFlag"));
		if("1".equals(ajaxFlag)){
//			Map<String,String> pramMap = new HashMap();
//			pramMap.put("begindate", A_Tools.NullToStr(request.getParameter("begindate")));
//			pramMap.put("enddate", A_Tools.NullToStr(request.getParameter("enddate")));
			String formObj="";
			try {
				//获取form表单
				formObj = URLDecoder.decode(A_Tools.NullToStr(request.getParameter("formObj")),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			Gson gson = new Gson();
			//将json格式的form表单转化为map
			Map paramMap = gson.fromJson(formObj, Map.class);			
			
			List<Map> list = this.dataShowService.queryAddrDateCharts(paramMap);
			//拼接折线图数据
			JsonObject rstJson = this.dataShowService.dealCurveChartsData(list);
			//最大最小及平均值
			List<Map> xlist = this.dataShowService.queryMaxMinAvgSec(paramMap);
			if(xlist!=null && xlist.size()>0){
				Map xmap = xlist.get(0);
				rstJson.addProperty("statistics", "最大值："+xmap.get("MAXSEC")+"；最小值："+xmap.get("MINSEC")+"；平均值："+xmap.get("AVGSEC"));
			}
			
			A_Tools.printJson(response,rstJson.toString());
		}
		return "myCharts";
	}
	@RequestMapping("/queryDelaySecDeviceNumCharts")
	public String queryDelaySecDeviceNumCharts(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//是否为ajax取数据标志
		String ajaxFlag = A_Tools.NullToStr(request.getParameter("ajaxFlag"));
		if("1".equals(ajaxFlag)){
			String formObj="";
			try {
				//获取form表单
				formObj = URLDecoder.decode(A_Tools.NullToStr(request.getParameter("formObj")),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			Gson gson = new Gson();
			//将json格式的form表单转化为map
			Map paramMap = gson.fromJson(formObj, Map.class);			

			//拼接柱状图数据
			JsonObject rstJson = this.dataShowService.dealBarChartsData(paramMap);
			A_Tools.printJson(response,rstJson.toString());
		}
		return "myCharts";
	}
	
	@RequestMapping("/queryDelaySecDeviceNumPercentCharts")
	public String queryDelaySecDeviceNumPercentCharts(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//是否为ajax取数据标志
		String ajaxFlag = A_Tools.NullToStr(request.getParameter("ajaxFlag"));
		if("1".equals(ajaxFlag)){
			String formObj="";
			try {
				//获取form表单
				formObj = URLDecoder.decode(A_Tools.NullToStr(request.getParameter("formObj")),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			Gson gson = new Gson();
			//将json格式的form表单转化为map
			Map paramMap = gson.fromJson(formObj, Map.class);			

			//拼接饼图数据
			JsonObject rstJson = this.dataShowService.dealPieChartsData(paramMap);
			A_Tools.printJson(response,rstJson.toString());
		}
		return "myCharts";
	}
		
	

}
